package com.joonhat.MonkeyFlash.hotkey;

import j2meunit.framework.Test;
import j2meunit.framework.TestCase;
import j2meunit.framework.TestMethod;
import j2meunit.framework.TestSuite;

import java.util.Enumeration;
import java.util.Vector;

import net.rim.device.api.system.ApplicationDescriptor;

public class HotkeyManagerTest extends TestCase{

	HotkeyManager hotkeyManager;
	
	public HotkeyManagerTest() {
	}

	public HotkeyManagerTest(String name, TestMethod testMethod) {
		super(name, testMethod);
	}

	protected void setUp() throws Exception {
		hotkeyManager = new HotkeyManager();
		super.setUp();
	}
	
	public void testGetDescriptorsFor1() {
		char hotKey = 0;
		Vector appList = hotkeyManager.getDescriptorsFor(hotKey);
		assertTrue(appList.size() == 0);
		Vector appList2 = hotkeyManager.getDescriptorsFor('\u0020');
		assertTrue(appList2.size() == 0);
	}
	
	public void testGetDescriptorsFor2() {
		Vector BList = hotkeyManager.getDescriptorsFor('B');
		Vector PList = hotkeyManager.getDescriptorsFor('P');
		Vector SList = hotkeyManager.getDescriptorsFor('S');
		Vector MList = hotkeyManager.getDescriptorsFor('M');
		Vector dList = hotkeyManager.getDescriptorsFor('d');
		
		assertTrue(BList.size() > 0);
		boolean equalBrowser = false;
		for (Enumeration enumList = BList.elements(); enumList.hasMoreElements(); ){
			if (((ApplicationDescriptor)enumList.nextElement()).getDescription().toString().equals("B\u0332rowser"))
				equalBrowser = true;
		}
		assertTrue("B\u0332rowser와 일치안함" ,equalBrowser);
		
		assertTrue(PList.size() > 0);
		boolean equalPhone = false;
		for (Enumeration enumList = PList.elements(); enumList.hasMoreElements() ;){
			if (((ApplicationDescriptor)enumList.nextElement()).getDescription().toString().equals("P\u0332hone"))
				equalPhone = true;
		}
		assertTrue("P\u0332hone와 일치안함" ,equalPhone);
		
		assertTrue(SList.size() > 0);
		boolean equarSearch = false;
		for (Enumeration enumList = SList.elements(); enumList.hasMoreElements(); ){
			if (((ApplicationDescriptor)enumList.nextElement()).getDescription().toString().equals("S\u0332earch"))
				equarSearch = true;
		}
		assertTrue("S\u0332earch와 일치안함" ,equarSearch);
		
		assertTrue(MList.size() > 0);
		boolean equalMessages = false;
		for (Enumeration enumList = MList.elements(); enumList.hasMoreElements(); ){
			if (((ApplicationDescriptor)enumList.nextElement()).getDescription().toString().equals("M\u0332essages"))
				equalMessages = true;
		}
		assertTrue("M\u0332essages와 일치안함", equalMessages);
		
		assertTrue(dList.size() > 0);
		boolean equalMemoPad = false;
		for (Enumeration enumList = dList.elements(); enumList.hasMoreElements(); ){
			if (((ApplicationDescriptor)enumList.nextElement()).getDescription().toString().equals("MemoPad\u0332"))
				equalMemoPad = true;
		}
		assertTrue("MemoPad\u0332와 일치안함" ,equalMemoPad);
	}
	
	public void testGetDescriptorsFor3() {
		Vector DList = hotkeyManager.getDescriptorsFor('D');
		Vector mList = hotkeyManager.getDescriptorsFor('m');
		Vector sList = hotkeyManager.getDescriptorsFor('s');
		
		assertTrue("대소문자 구별 테스트:\n 누군가의 description에 D가 저장된다면 실패할수도 있음",
				DList.size() == 0);
		assertTrue("대소문자 구별 테스트:\n 누군가의 description에 m이 저장된다면 실패할수도 있음",
				mList.size() == 0);
		assertTrue("대소문자 구별 테스트:\n 누군가의 description에 s가 저장된다면 실패할수도 있음",
				sList.size() == 0);
	}
	
//	Rim에서 만들이진 앱이 아니라 일반사용자가 만든 앱의 단축키도 잘 추출하는지 테스트
//  Rim에서 만든앱은 Descriontion에 일반사용자가만든 앱은 Name에 단축키가 저장되어있다
//  일반사용자가 만든 앱들은 수시로 변하므로 주석으로 처리해놈
//	public void testGetDescriptorsFor4() {
//		
//		Vector myApp = hotkeyManager.getDescriptorsFor('t');
//		
//		assertTrue(myApp.size() > 0);
//		boolean equalMyApp = false;
//		for (Enumeration enumList = myApp.elements(); enumList.hasMoreElements(); ){
//			if (((ApplicationDescriptor)enumList.nextElement()).getName().equals("MonkeyFlashStartup t\u0332"))
//				equalMyApp = true;
//		}
//		assertTrue("내 단축키가 t가 아님" ,equalMyApp);
//		
//	}
	
	
	public void testHasNotAtCharUnderBar1(){
		// 단축키로 아무것도 입력되지 않았을때(0)
		char zero = 0;
		assertTrue("char가 0일때", hotkeyManager.hasNotAtCharUnderBar(zero, "ABCDEFG"));
		assertTrue("char가 0일때", hotkeyManager.hasNotAtCharUnderBar(zero, "\u0332ABCDEFG"));
		assertTrue("char가 0일때", hotkeyManager.hasNotAtCharUnderBar(zero, "A\u0332BCDEFG"));
		assertTrue("char가 0일때", hotkeyManager.hasNotAtCharUnderBar(zero, "AB\u0332CDEFG"));
		assertTrue("char가 0일때", hotkeyManager.hasNotAtCharUnderBar(zero, "ABC\u0332DEFG"));
		assertTrue("char가 0일때", hotkeyManager.hasNotAtCharUnderBar(zero, "ABCD\u0332EFG"));
		assertTrue("char가 0일때", hotkeyManager.hasNotAtCharUnderBar(zero, "ABCD\u0332EFG\u0332"));
	}
	
	public void testHasNotAtCharUnderBar2(){
		assertTrue("언더바 없음", hotkeyManager.hasNotAtCharUnderBar('A', "AB CDEFG"));
		assertTrue("언더바 없음", hotkeyManager.hasNotAtCharUnderBar('B', "ABCD EFG"));
		assertTrue("언더바 없음", hotkeyManager.hasNotAtCharUnderBar('C', " ABCDEFG"));
		assertTrue("언더바 없음", hotkeyManager.hasNotAtCharUnderBar('D', "ABCDEFG"));
		assertTrue("언더바 없음", hotkeyManager.hasNotAtCharUnderBar('G', "AB CDEFG"));
	}
	
	public void testHasNotAtCharUnderBar3(){
		assertTrue("언더바 있음", !hotkeyManager.hasNotAtCharUnderBar('A', "A\u0332BCDEFG"));
		assertTrue("언더바 있음", !hotkeyManager.hasNotAtCharUnderBar('B', "AB\u0332CDEFG"));
		assertTrue("언더바 있음", !hotkeyManager.hasNotAtCharUnderBar('C', "ABC\u0332DEFG"));
		assertTrue("언더바 있음", !hotkeyManager.hasNotAtCharUnderBar('D', "ABCD\u0332EFG"));
		assertTrue("언더바 있음", !hotkeyManager.hasNotAtCharUnderBar('E', "ABCDE\u0332FG"));
		assertTrue("언더바 있음", !hotkeyManager.hasNotAtCharUnderBar('F', "ABCDEF\u0332G"));
		assertTrue("언더바 있음", !hotkeyManager.hasNotAtCharUnderBar('G', "ABCDEFG\u0332"));
	}
	
	
	public Test suite() {
		TestSuite suite = new TestSuite("HotkeyManager");
		
		suite.addTest(new HotkeyManagerTest("getDescriptorsFor1", new TestMethod()
		{ public void run(TestCase tc) throws Throwable { ((HotkeyManagerTest)tc).testGetDescriptorsFor1(); } }));
		suite.addTest(new HotkeyManagerTest("getDescriptorsFor2", new TestMethod()
		{ public void run(TestCase tc) throws Throwable { ((HotkeyManagerTest)tc).testGetDescriptorsFor2(); } }));
		suite.addTest(new HotkeyManagerTest("getDescriptorsFor3", new TestMethod()
		{ public void run(TestCase tc) throws Throwable { ((HotkeyManagerTest)tc).testGetDescriptorsFor3(); } }));
//		suite.addTest(new HotkeyManagerTest("testGetDescriptorsFor4", new TestMethod()
//		{ public void run(TestCase tc) throws Throwable { ((HotkeyManagerTest)tc).testGetDescriptorsFor4(); } }));
		
		suite.addTest(new HotkeyManagerTest("hasNotAtCharUnderBar1", new TestMethod() {
			public void run(TestCase tc) throws Throwable { ((HotkeyManagerTest)tc).testHasNotAtCharUnderBar1(); } }));
		suite.addTest(new HotkeyManagerTest("hasNotAtCharUnderBar2", new TestMethod() {
			public void run(TestCase tc) throws Throwable { ((HotkeyManagerTest)tc).testHasNotAtCharUnderBar2(); } }));
		suite.addTest(new HotkeyManagerTest("hasNotAtCharUnderBar3", new TestMethod() {
			public void run(TestCase tc) throws Throwable { ((HotkeyManagerTest)tc).testHasNotAtCharUnderBar3(); } }));
		
		return suite;
	}
	
}
